﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

using CPDPWEM.Utils;
using Functionality;

namespace Visual.ViewModels
{
    public class AddPersonViewModel : ViewModelBase
    {
        #region Binding Properties

        public ICommand AddPersonCommand
        {
            get { return _AddPersonCommand; }
        }

        public string FullPersonName
        {
            get { return _FullPersonName; }
            set
            {
                if (_FullPersonName != value)
                {
                    _FullPersonName = value;
                    _AddPersonCommand.RaiseCanExecuteChanged();
                }
            }
        }

        /// <summary>
        /// Set to TRUE to return focus to the FullPersonName TextBox (via attached property).
        /// </summary>
        public bool IsTextBoxFocused
        {
            get { return _isTextBoxFocused; }
            set { _isTextBoxFocused = value; OnPropertyChanged("IsTextBoxFocused"); }
        }

        /// <summary>
        /// Oddly enough, just can't return _People, need to actually perform some sort of transform
        /// on the collection _People before returning it.
        /// </summary>
        public IEnumerable<Person> People
        {
            get
            {
                return from person in _People
                       select person;
            }
        }

        #endregion

        /// <summary>
        /// Default constructor
        /// </summary>
        public AddPersonViewModel()
        {
            _FullPersonName = String.Empty;
            _People = new List<Person>();
            _AddPersonCommand = new Command(AddPerson, CanAddPerson);
            IsTextBoxFocused = true;
        }

        #region AddPerson Command

        /// <summary>
        /// Adds a person
        /// </summary>
        void AddPerson()
        {            
            _People.Add(new Person(FullPersonName));

            // Reset the form for quick entry.
            FullPersonName = String.Empty;
            IsTextBoxFocused = true; // Return focus, via attached property, to the TextBox

            OnPropertyChanged("People");
            OnPropertyChanged("FullPersonName");
        }

        /// <summary>
        /// Determins if a person can be added. Does not allow duplicates.
        /// </summary>
        /// <returns>TRUE if the user can add a person.</returns>
        bool CanAddPerson()
        {
            // Need to have entered something
            if (String.IsNullOrEmpty(FullPersonName))
                return false;

            // Can't add duplicates
            Person person = new Person(FullPersonName);
            if (_People.Contains(person, new PersonCompare()))
                return false;

            return true;
        }

        #endregion

        #region Private

        Command _AddPersonCommand;
        string _FullPersonName;
        List<Person> _People;
        bool _isTextBoxFocused;

        #endregion
    }
}
